<template>
	<view class="inner">
		<view>
			<view class="text">
				VIP最新推荐
			</view>
			<scroll-view scroll-y="true" scroll-x="true" class="scroll">
				<view class="scroll-outer">
					<view class="rel-item" v-for="item in NavData" :key="item._id">
						<image class="main-img" @tap="ImgClick(item._id)" :src="item.coverpic" mode=""></image>
						<view>
							<view class="item-text-one">{{item.name}}</view>
							<view class="item-text">
								<view class="left">
									<text class="grey">{{item.collections}}</text>
									<image class="img-one" src="../../static/vip/eye.png" mode=""></image>
								</view>
								<view class="right">
									<text class="grey">{{item.pageview}}</text>
									<image class="img-one" src="../../static/vip/star.png" mode=""></image>
								</view>
							</view>
						</view>
						<text class="scroll-item">{{item.time  ? item.time  : '00:00'}}</text>
					</view>
					
				</view>
			</scroll-view>
		</view>
		<view>
			<view class="text">
				限时免费体验
			</view>
			<scroll-view scroll-y="true" scroll-x="true">
				<view class="scroll-outer">
					<view class="rel-item" v-for="item in NavData2" :key="item._id">
						<image class="main-img" @tap="ImgClick(item._id)" :src="item.coverpic" mode=""></image>
						<view>
							<view class="item-text-one">{{item.name}}</view>
							<view class="item-text">
								<view class="left">
									<text class="grey">{{item.collections}}</text>
									<image class="img-one" src="../../static/vip/eye.png" mode=""></image>
								</view>
								<view class="right">
									<text class="grey">{{item.pageview}}</text>
									<image class="img-one" src="../../static/vip/star.png" mode=""></image>
								</view>
							</view>
						</view>
						<text class="scroll-item">{{item.time  ? item.time  : '00:00'}}</text>
					</view>



				</view>
			</scroll-view>
		</view>

		<view>
			<view class="text">
				猜你喜欢
			</view>
			<view class="scroll-outer2">
				<view class="item-outer" v-for="item in NavData3" :key="item._id">
					<image class="main-img" :src="item.coverpic" mode=""></image>
					<view>
						<view class="item-text-one">{{item.name}}</view>
						<view class="item-text">
							<view class="left">
								<text class="grey">{{item.collections}}</text>
								<image class="img-one" src="../../static/vip/eye.png" mode=""></image>
							</view>
							<view class="right">
								<text class="grey">{{item.pageview}}</text>
								<image class="img-one" src="../../static/vip/star.png" mode=""></image>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import request from '../../http/request2.js'
	export default {
		data() {
			return {
				NavData: [],
				NavData2: [],
				NavData3: [],
				TimeData:[],

			}
		},
		methods: {
			ImgClick(id) {
				console.log(id)
				uni.navigateTo({
					url: `../jelly/index?id=${id}`
				})
			},
			async setNavData() {
				const res = await request({
					url: "/vip/getRecommendMenuList"
				});
				this.NavData = res.menus;
				//
				this.getTime(this.NavData)
			},
			async setNavData2() {
				const res = await request({
					url: "/vip/getisFreeMenuList"
				});
				this.NavData2 = res.menus;
				this.getTime(this.NavData2)
			},
			async setNavData3() {
				const res = await request({
					url: "/vip/likeMenu"
				});
				// console.log(res)
				this.NavData3 = res.menus;
				// this.getTime(this.NavData3)
			},
			formatSeconds(value) {
				let theTime = parseInt(value); // 秒
				let middle = 0; // 分
				if (theTime > 60) {
					middle = parseInt(theTime / 60);
					theTime = parseInt(theTime % 60);
				}
				return `${middle>9?middle:'0'+middle}′${theTime>9?theTime:'0'+theTime}″`;
			},

			getTime(arr) {
					// console.log(arr)
				arr.forEach(item => {
				
					let audioContext = uni.createInnerAudioContext();
					audioContext.volume = 0;
					audioContext.autoplay = true;
					
					audioContext.src = item.vid;
					let that = this;
					// 必须写在onCanplay里面，否则获取不到
					audioContext.onCanplay(() => {
						audioContext.duration;
						audioContext.buffered;
						//如果不写上面2行，打印audioContext，duration，buffered都有值，但打印audioContext.duration或audioContext.buffered值为0
						setTimeout(() => {
							if (audioContext.duration * 1 > 0 || audioContext.buffered *
								1 > 0) {
								// 部分iphone手机audioContext.duration 会为为0，所以判断audioContext.duration是否为0
								that.videoTime = that.formatSeconds(audioContext
									.duration == 0 ?
									audioContext.buffered :
									audioContext.duration);
							}
							this.$set(item,'time',that.videoTime)
							// console.log(item)
						}, 1000)
					})
				})
			}
		},
		created() {
			this.setNavData();
			this.setNavData2()
			this.setNavData3();
			// console.log(this.$store.state.token,'----------')
		},
		
		onLoad() {
			
		}
	}
</script>

<style scoped>
	.inner {
		width: 710rpx;
		margin: 0 auto;
	}

	.text {
		font-size: 40rpx;
		font-weight: bold;
		letter-spacing: 2px;
		margin-top: 20rpx;
	}

	.main-img {
		width: 315rpx;
		height: 220rpx;
		border-radius: 10rpx;
	}

	.scroll-outer {
		display: flex;

	}

	.scroll-outer2 {
		display: flex;
		flex-wrap: wrap;
		margin-top: 28rpx;
		justify-content: space-between
	}

	.scroll-outer>view {
		margin-right: 18rpx;
		margin-top: 28rpx;
	}

	.item-outer {
		width: 49%;
		margin-bottom: 20rpx;

	}

	.item-outer>.main-img {
		width: 95%;
	}

	.rel-item {
		position: relative;
	}

	.scroll-item {
		width: 80rpx;
		height: 22rpx;
		line-height: 22rpx;
		display: inline-block;
		/* position: relative; */
		position: absolute;
		top: 94px;
		right: 7px;
		font-size: 15rpx;
		background-color: #999999;
		text-align: center;
		color: #FFFFFF;
	}

	.item-text {
		display: flex;
	}

	.item-text-one {
		font-size: 25rpx;
		font-weight: bold;

		width: 300rpx;
		white-space: nowrap; 
		text-overflow: ellipsis;

		overflow: hidden; 
	}

	.img-one {
		width: 25rpx;
		height: 25rpx;
		vertical-align: middle;
		margin-left: 10rpx;
	}

	.grey {
		color: rgb(177, 177, 177);
		font-size: 10px;
		vertical-align: middle;

	}

	.right {
		margin-left: 64rpx;
	}

	.scroll {
		width: 100%;
		white-space: nowrap;
	}
	.pos{
		display: flex;
		position: relative;
		z-index: 5;
	}
</style>
